package cn.edu.klmy.dao.impl;

import cn.edu.klmy.dao.IStudentClaszRelDao;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Repository
public class StudentClaszRelDao extends JdbcDaoSupport implements IStudentClaszRelDao {

    private static final String ADD_STUDENTS_TO_ADMIN_CLASZ = "UPDATE STUDENT SET ADMIN_CLASS_ID = ? WHERE ID = ?";
    private static final String ADD_STUDENTS_TO_STUDY_CLASZ = "UPDATE STUDENT SET STUDY_CLASS_ID = ? WHERE ID = ?";

    @Override
    public void addStudentsToClasz(final int[] studentIds, final int claszId, int claszType) {
        if (0 == claszType) {
            arrist(ADD_STUDENTS_TO_STUDY_CLASZ, studentIds, claszId);
        } else {
            arrist(ADD_STUDENTS_TO_ADMIN_CLASZ, studentIds, claszId);
        }
    }

    private static final String REMOVE_STUDENTS_TO_ADMIN_CLASZ = "UPDATE STUDENT SET ADMIN_CLASS_ID = NULL WHERE ADMIN_CLASS_ID = ? AND ID = ?";
    private static final String REMOVE_STUDENTS_TO_STUDY_CLASZ = "UPDATE STUDENT SET STUDY_CLASS_ID = NULL WHERE STUDY_CLASS_ID = ? AND ID = ?";

    @Override
    public void removeStudentsFromClasz(int[] studentIds, int claszId, int claszType) {
        if (0 == claszType) {
            arrist(REMOVE_STUDENTS_TO_STUDY_CLASZ, studentIds, claszId);
        } else {
            arrist(REMOVE_STUDENTS_TO_ADMIN_CLASZ, studentIds, claszId);
        }
    }

    private static final String REMOVE_STUDENT_FROM_ALL_CLASZS = "UPDATE STUDENT SET ADMIN_CLASS_ID = NULL, STUDY_CLASS_ID = NULL WHERE ID = ?";

    @Override
    public void removeStudentFromAllClasz(int studentId) {
        getJdbcTemplate().update(REMOVE_ALL_STUDENTS_FROM_ADMIN_CLASZ, new Object[]{studentId});
    }

    private void arrist(String sql, int[] studentIds, Integer claszId) {
        getJdbcTemplate().batchUpdate(sql, new MyBatchPreparedStatementSetter(studentIds, claszId));
    }

    private static final String REMOVE_ALL_STUDENTS_FROM_ADMIN_CLASZ = "UPDATE STUDENT SET ADMIN_CLASS_ID = NULL WHERE ADMIN_CLASS_ID = ?";
    private static final String REMOVE_ALL_STUDENTS_FROM_STUDY_CLASZ = "UPDATE STUDENT SET STUDY_CLASS_ID = NULL WHERE STUDY_CLASS_ID = ?";

    @Override
    public void removeAllStudentFromClasz(int claszId, int claszType) {
        if (claszType == 1)
            getJdbcTemplate().update(REMOVE_ALL_STUDENTS_FROM_ADMIN_CLASZ, new Object[]{claszId});
        else
            getJdbcTemplate().update(REMOVE_ALL_STUDENTS_FROM_STUDY_CLASZ, new Object[]{claszId});
    }

    @Resource(name = "dataSource")
    public void setSuperDataSource(DataSource dataSource) {
        super.setDataSource(dataSource);
    }

    private static class MyBatchPreparedStatementSetter implements BatchPreparedStatementSetter {

        private int[] studentIds;
        private int claszId;

        public MyBatchPreparedStatementSetter(int[] studentIds, int claszId) {
            this.studentIds = studentIds;
            this.claszId = claszId;
        }

        @Override
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
            preparedStatement.setInt(1, claszId);
            preparedStatement.setInt(2, studentIds[i]);
        }

        @Override
        public int getBatchSize() {
            return studentIds.length;
        }
    }
}
